from flask import Blueprint, request, jsonify

from service import modelService_v4

model_v4_bp = Blueprint('model_v4', __name__, url_prefix='/model_v4')
@model_v4_bp.route('/querySingleQuestion', methods=['POST'])
def querySingleQuestion():
    data = request.get_json()
    question = data.get('question')
    answer = data.get('answer')
    ai_score_precision, ai_score_complete, ai_score_useful = modelService_v4.run_eval_exp(question,answer)
    return jsonify({
        "msg":"success",
        "code": 0,
        "data": {
            "precision": ai_score_precision,
            "complete": ai_score_complete,
            "useful": ai_score_useful
        }
    })


# def run_eval_exp():
#     question = question_exp
#     answer = answer_exp
#     ai_score_precsion, ai_score_complete, ai_score_useful = 0, 0, 0
#     user_config = {"configurable": {"thread_id": '0'}}
#     for event in graph.stream(
#             {"messages": [''], "question": question, "answer": answer},
#             user_config,
#             stream_mode="updates"
#         ):
#             for value in event.values():
#                 if 'search_agent_node' in event.keys():
#                     try:
#                         print("Assistant1:\n{}".format(value["messages"][-1].content))
#                     except:
#                         pass
#                 elif 'summary_agent_node' in event.keys():
#                     try:
#                         print("Assistant2:\n{}".format(value["messages"][-1].content))
#                     except:
#                         pass
#                 elif 'eval_agent_node' in event.keys():
#                     try:
#                         print("Expert:\n{}".format(value["messages"][-1].content))
#                     except:
#                         pass
#                     socre_pred_str = value["messages"][-1].content
#                     socre_pred_str = re.sub(r'^```json\n', '', socre_pred_str, count=1)
#                     socre_pred_str = re.sub(r'\n```$', '', socre_pred_str, count=1)
#                     socre_pred = json.loads(socre_pred_str)
#                     ai_score_precsion = int(socre_pred['准确性'])
#                     ai_score_complete = int(socre_pred['全面性'])
#                     ai_score_useful = int(socre_pred['实用性'])
#                 else:
#                     try:
#                         print("Others:\n{}".format(value["messages"][-1].content))
#                     except:
#                         pass
#                 print('\n')

#     return ai_score_precsion, ai_score_complete, ai_score_useful